<!DOCTYPE html>
<html>
<head>
    <title>Physics Diagram</title>
</head>
<body>
<canvas id="physicsCanvas" width="800" height="900"></canvas>
<script>
    const canvas = document.getElementById('physicsCanvas');
    const ctx = canvas.getContext('2d');

    function drawFig1() {
        ctx.save();
        ctx.strokeStyle = 'black';
        ctx.fillStyle = 'black';
        ctx.font = '16px Arial';
        ctx.textAlign = 'center';

        // Sphere
        const sphere_cx = 350;
        const sphere_cy = 130;
        const sphere_r = 50;
        ctx.beginPath();
        ctx.arc(sphere_cx, sphere_cy, sphere_r, 0, 2 * Math.PI);
        ctx.lineWidth = 1.5;
        ctx.stroke();

        // Labels for sphere
        ctx.textAlign = 'right';
        ctx.fillText('sphere,', 280, 115);

        const chargeX = 280;
        const chargeY = 135;
        ctx.font = 'italic 16px Arial';
        const w_q = ctx.measureText('q').width;
        ctx.fillText('q', chargeX, chargeY);
        ctx.font = '16px Arial';
        ctx.fillText('charge ', chargeX - w_q, chargeY);
        
        ctx.beginPath();
        ctx.moveTo(285, 125);
        ctx.lineTo(300, 125);
        ctx.stroke();


        // Point P
        const p_x = sphere_cx + 250;
        const p_y = sphere_cy;
        ctx.beginPath();
        ctx.arc(p_x, p_y, 3, 0, 2 * Math.PI);
        ctx.fill();
        ctx.textAlign = 'left';
        ctx.fillText('P', p_x + 10, p_y + 5);

        // Dashed line through center
        ctx.beginPath();
        ctx.setLineDash([5, 5]);
        ctx.moveTo(sphere_cx - sphere_r - 20, sphere_cy);
        ctx.lineTo(p_x + 50, p_y);
        ctx.stroke();
        ctx.setLineDash([]);

        // Distance x indicator
        const arrow_y = sphere_cy - 80;
        ctx.beginPath();
        ctx.moveTo(sphere_cx, arrow_y);
        ctx.lineTo(p_x, arrow_y);
        // Arrowheads
        ctx.moveTo(sphere_cx, arrow_y);
        ctx.lineTo(sphere_cx + 10, arrow_y - 5);
        ctx.moveTo(sphere_cx, arrow_y);
        ctx.lineTo(sphere_cx + 10, arrow_y + 5);
        ctx.moveTo(p_x, arrow_y);
        ctx.lineTo(p_x - 10, arrow_y - 5);
        ctx.moveTo(p_x, arrow_y);
        ctx.lineTo(p_x - 10, arrow_y + 5);
        ctx.stroke();

        // Dashed lines to arrow
        ctx.beginPath();
        ctx.setLineDash([5, 5]);
        ctx.moveTo(sphere_cx, sphere_cy);
        ctx.lineTo(sphere_cx, arrow_y);
        ctx.moveTo(p_x, p_y);
        ctx.lineTo(p_x, arrow_y);
        ctx.stroke();
        ctx.setLineDash([]);

        // Label x
        ctx.textAlign = 'center';
        ctx.font = 'italic 16px Arial';
        ctx.fillText('x', sphere_cx + (p_x - sphere_cx) / 2, arrow_y - 10);
        
        // Figure label
        ctx.font = 'bold 16px Arial';
        ctx.fillText('Fig. 6.1', sphere_cx + 100, sphere_cy + 120);
        
        ctx.restore();
    }

    function drawFig2() {
        ctx.save();
        ctx.strokeStyle = 'black';
        ctx.fillStyle = 'black';
        ctx.font = '16px Arial';
        
        const originX = 100;
        const originY = 780;
        const graphWidth = 600;
        const graphHeight = 420;

        const xMax = 12;
        const yMax = 14;

        const scaleX = graphWidth / xMax; // 50 px per cm
        const scaleY = graphHeight / yMax; // 30 px per 10^3 V

        // Draw grid
        ctx.lineWidth = 0.5;
        ctx.strokeStyle = '#b0b0b0';

        // Minor vertical lines (every 0.2 cm)
        for (let i = 0; i <= xMax / 0.2; i++) {
            const x = originX + i * 0.2 * scaleX;
            ctx.beginPath();
            ctx.moveTo(x, originY);
            ctx.lineTo(x, originY - graphHeight);
            ctx.stroke();
        }

        // Minor horizontal lines (every 0.2 V unit)
        for (let i = 0; i <= yMax / 0.2; i++) {
            const y = originY - i * 0.2 * scaleY;
            ctx.beginPath();
            ctx.moveTo(originX, y);
            ctx.lineTo(originX + graphWidth, y);
            ctx.stroke();
        }
        
        // Major grid lines
        ctx.lineWidth = 1;
        ctx.strokeStyle = 'black';

        // Major vertical grid lines
        for (let i = 0; i <= xMax / 2; i++) {
            const x = originX + i * 2 * scaleX;
            ctx.beginPath();
            ctx.moveTo(x, originY);
            ctx.lineTo(x, originY - graphHeight);
            ctx.stroke();
        }

        // Major horizontal grid lines
        for (let i = 0; i <= yMax / 2; i++) {
            const y = originY - i * 2 * scaleY;
            ctx.beginPath();
            ctx.moveTo(originX, y);
            ctx.lineTo(originX + graphWidth, y);
            ctx.stroke();
        }

        // Draw axes
        ctx.lineWidth = 1.5;
        ctx.beginPath();
        ctx.moveTo(originX, originY);
        ctx.lineTo(originX, originY - graphHeight - 10);
        ctx.stroke();
        ctx.beginPath();
        ctx.moveTo(originX - 10, originY);
        ctx.lineTo(originX + graphWidth, originY);
        ctx.stroke();

        // Axis Ticks
        ctx.fillStyle = 'black';
        ctx.font = '14px Arial';
        ctx.textAlign = 'center';
        ctx.textBaseline = 'top';
        for (let i = 0; i <= xMax / 2; i++) {
            const xVal = i * 2;
            if (xVal === 0) continue;
            const x = originX + xVal * scaleX;
            ctx.fillText(xVal, x, originY + 5);
        }
        ctx.fillText(0, originX, originY + 5);

        ctx.textAlign = 'right';
        ctx.textBaseline = 'middle';
        for (let i = 0; i <= yMax / 2; i++) {
            const yVal = i * 2;
             if (yVal === 0) continue;
            const y = originY - yVal * scaleY;
            ctx.fillText(yVal, originX - 15, y);
        }

        // Axis Labels
        const fontItalic = 'italic 16px Arial';
        const fontNormal = '16px Arial';

        // x-axis label: x / cm
        ctx.textAlign = 'center';
        ctx.textBaseline = 'top';
        const xLabelY = originY + 35;
        const xLabelX = originX + graphWidth / 2;
        ctx.font = fontItalic;
        const wx = ctx.measureText('x').width;
        ctx.font = fontNormal;
        const wrestx = ctx.measureText(' / cm').width;
        const totalWx = wx + wrestx;
        ctx.font = fontItalic;
        ctx.fillText('x', xLabelX - totalWx / 2 + wx / 2, xLabelY);
        ctx.font = fontNormal;
        ctx.fillText(' / cm', xLabelX - totalWx / 2 + wx + wrestx / 2, xLabelY);

        // y-axis label: V / 10³ V
        ctx.save();
        const yLabelX = originX - 65;
        const yLabelY = originY - graphHeight / 2;
        ctx.translate(yLabelX, yLabelY);
        ctx.rotate(-Math.PI / 2);
        ctx.textAlign = 'center';
        ctx.textBaseline = 'bottom';
        ctx.font = fontItalic;
        const wV = ctx.measureText('V').width;
        ctx.font = fontNormal;
        const wrestV = ctx.measureText(' / 10³ V').width;
        const totalWV = wV + wrestV;
        ctx.font = fontItalic;
        ctx.fillText('V', -totalWV / 2 + wV / 2, 0);
        ctx.font = fontNormal;
        ctx.fillText(' / 10³ V', -totalWV / 2 + wV + wrestV / 2, 0);
        ctx.restore();

        // Draw the curve V(x)
        ctx.beginPath();
        ctx.lineWidth = 2.0;
        ctx.strokeStyle = 'black';

        // Part 1: Constant potential V = 13
        const V_const = 13;
        const R = 2.5; // Radius in cm
        ctx.moveTo(originX, originY - V_const * scaleY);
        ctx.lineTo(originX + R * scaleX, originY - V_const * scaleY);
        
        // Part 2: V = (V_const * R) / x
        const k = V_const * R; // 13 * 2.5 = 32.5
        for (let x = R; x <= xMax; x += 0.05) {
            const V = k / x;
            const px = originX + x * scaleX;
            const py = originY - V * scaleY;
            ctx.lineTo(px, py);
        }
        ctx.stroke();

        // Figure label
        ctx.font = 'bold 16px Arial';
        ctx.textAlign = 'center';
        ctx.textBaseline = 'alphabetic';
        ctx.fillText('Fig. 6.2', originX + graphWidth / 2, originY + 75);

        ctx.restore();
    }

    // Main drawing execution
    ctx.fillStyle = 'white';
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    
    drawFig1();
    drawFig2();

</script>
</body>
</html>